#include "stdafx.h"
#include "fileIter.h"
#include <Magick++.h> 
#include <boost/xpressive/xpressive.hpp>
#include <boost/xpressive/regex_actions.hpp>
#include "tiff2pdf.h"
using namespace std;
//using namespace boost::filesystem;
using namespace boost::xpressive;

namespace fs = boost::filesystem;

fileIter::fileIter(void)
{

}

int fileIter::IterTiff(const fs::path & dir_path, fs::path & path_found)
{
  string commandStr;
  mark_tag tiff(1);
	 
  cregex tiffFileName = (tiff= +(_w | _d))
		   >> '.' 
           >> "tiff" ;

  cmatch what;

  if ( !exists( dir_path ) ) 
  {
	  cout << "directory does not exist" << '\n' ;
	  exit(-1);
  }

  fs::directory_iterator end_itr; // default construction yields past-the-end
  vector<string> filePathes;

  for ( fs::directory_iterator itr( dir_path );
        itr != end_itr;
        ++itr )
  {

    
    if ( is_directory(itr->status()) )
    {
	  commandStr = string("convert ")+itr->string()+string("/")+string("*.tiff")+string(" ")+
		  itr->string()+string("/")+itr->leaf()+string(".pdf");
	  system(commandStr.c_str());
	  
      IterTiff( itr->path(),path_found);
	    
    }
    else// see below
    {
	  return 0;
	  string thisFile = itr->leaf();

      if(regex_search(thisFile.c_str(), what, tiffFileName))
		    {
		        cout << what[0] << '\n'; // whole match
				//FileCurrMode[fileIndex] = atoi(what[mode].str().c_str());
				path_found = itr->path();
				string PathStr = path_found.string();
				filePathes.push_back(PathStr);
		    } 
    }
  }
 
  tiff2pdf convert(filePathes);


	return 0;
}


fileIter::~fileIter(void)
{
}
